var express = require('express');
var router = express.Router();
// const { formidable } = require('formidable')
const { AccountModel } = require('../model/index.js')
const authCheckMiddleware = require('../middleware/auth.js')
// const mongoose = require('mongoose')
// const AccountSchema = mongoose.Schema({
//   username: String,
//   dec: String
// })
// const AccountModel = mongoose.model('account', AccountSchema)
// const UserSchema = mongoose.Schema({
//   username: String,
//   password: String
// })
// const UserModel = mongoose.model('users', UserSchema)
// const low = require('lowdb')
// const FileSync = require('lowdb/adapters/FileSync')
// const adapter = new FileSync('data/db.json')
// const db = low(adapter)
// const shortid = require('shortid');
 
/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

/* 上传页面 */
router.get('/portrait', function(req, res, next) {
  res.render('portrait');
});
/* 注册 */
router.get('/register', async function(req, res, next) {
  res.render('register')
});
/* 登录 */
router.get('/login', async function(req, res, next) {
  res.render('login')
  // if(req.query.username === 'admin' && req.query.password === 'admin') {
  //   req.session.username = 'admin'
  //   res.send('登录成功')
  // } else {
  //   res.send('登录失败')
  // }
});
// /* 列表 */
router.get('/account', authCheckMiddleware, async function(req, res, next) {
  const accountQuery = await AccountModel.find()
  res.render('account', { account: accountQuery });
});
/* 创建列表 */
router.get('/account/create', authCheckMiddleware, function(req, res, next) {
  res.render('create');
});
/* 删除列表 */
router.get('/account/delete/:id',authCheckMiddleware, async function(req, res, next) {
  const response = await AccountModel.deleteOne({ _id: req.params.id })
  if(!response) return res.status = 500  
  res.render('transferPage', { status: '删除成功', backUrl: '/account' })
});


module.exports = router;
